package com.bw.zg3park2212.device

import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.databinding.ActivityDeviceBinding
import com.bw.zg3park2212.adpter.DeviceAdpter
import com.bw.zg3park2212.device.DeviceViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path ="/app/DeviceActivity")
class DeviceActivity : BaseActivity<ActivityDeviceBinding,DeviceViewModel>() {
    lateinit var deviceAdpter: DeviceAdpter
    var search=""
    override fun intitView() {
        deviceAdpter= DeviceAdpter()
        binding.rv.adapter=deviceAdpter
        binding.rv.layoutManager=LinearLayoutManager(this)
        binding.sv.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (query!=null){
                    search=query
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                search=""
                viewmodel.getDevice()
                return false
            }
        })
        binding.btnAddress.setOnClickListener {
            if (search.isEmpty()){
                ToastUtils.showLong("请输入搜索地址")
                return@setOnClickListener
            }else{
                viewmodel.getDeviceAddress(search)
            }
        }
        binding.btnName.setOnClickListener {
            if (search.isEmpty()){
                ToastUtils.showLong("请输入搜索名称")
                return@setOnClickListener
            }else{
                viewmodel.getDeviceName(search)
            }
        }
        ItemTouchHelper(object :ItemTouchHelper.Callback(){
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
            ): Int {
                val left=ItemTouchHelper.LEFT
                return makeMovementFlags(0,left)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder,
            ): Boolean {
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.layoutPosition
                viewmodel.getDeviceDel(deviceAdpter.data[position].id)
                deviceAdpter.remove(position)
            }
        }).attachToRecyclerView(binding.rv)
        deviceAdpter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/DeviceXiangActivity")
                .withString("dxid",deviceAdpter.data[position].id.toString())
                .navigation()
        }
    }

    override fun intitData() {
        viewmodel.deviceSuccse.observe(this){
            deviceAdpter.data.clear()
            deviceAdpter.addData(it)
            deviceAdpter.notifyDataSetChanged()
        }
        viewmodel.deviceaddress.observe(this){
            deviceAdpter.data.clear()
            deviceAdpter.addData(it)
            deviceAdpter.notifyDataSetChanged()
        }
        viewmodel.devicename.observe(this){
            deviceAdpter.data.clear()
            deviceAdpter.addData(it)
            deviceAdpter.notifyDataSetChanged()
        }
        viewmodel.devicedel.observe(this){
            ToastUtils.showLong("删除成功")
        }
    }

    override fun onResume() {
        super.onResume()
        viewmodel.getDevice()
    }
}